﻿using System;
using System.Web;
using CNDM.BO.GetData;
using System.Web.Script.Serialization;

namespace CNDM.GETDATA
{
    /// <summary>
    /// Summary description for getdata
    /// </summary>
    public class getdata : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            /*
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
            */
            string value ="";
            if (context.Request["k"]!=null && context.Request["k"].ToString() == "cat")
            {
                value = GetAllCategory();
            }
            else if (context.Request["k"] != null && context.Request["k"] == "detail" && context.Request["newsid"] != null)
            {
                int topOther = 5;
                if (context.Request["top"] != null && context.Request["top"].ToString() != "")
                    topOther = Convert.ToInt32(context.Request["top"]);
                value = GetNewsDetail(Convert.ToInt64(context.Request["newsid"].ToString()), topOther);
                
            }
            else if (context.Request["k"] != null && context.Request["k"] == "list" && context.Request["catid"] != null && context.Request["pageindex"] != null && context.Request["pagesize"]!=null)
            {
                //value = GetNewsDetail(Convert.ToInt64(context.Request["newsid"].ToString()));
                int pageindex = Convert.ToInt32(context.Request["pageindex"]);
                int pagesize = Convert.ToInt32(context.Request["pagesize"]);
                string catid = context.Request["catid"].ToString();
                value = GetNewsList(catid, pageindex, pagesize);
            }
            else if (context.Request["k"] != null && context.Request["k"] == "search" && context.Request["s"] != null && context.Request["s"].ToString() != "" && context.Request["pageindex"] != null && context.Request["pagesize"] != null)
            {
                string keysearch = context.Request["s"].ToString();
                int pageindex = Convert.ToInt32(context.Request["pageindex"]);
                int pagesize = Convert.ToInt32(context.Request["pagesize"]);
                value = Search(keysearch, pageindex, pagesize);

            }
            else if (context.Request["k"] != null && context.Request["k"]=="nbtc")
            {
                int top = 6;
                if(context.Request["top"]!=null && context.Request["top"].ToString()!="")
                {
                    top = Convert.ToInt32(context.Request["top"].ToString());
                }

                value = NoiBatTrangChu(top);
            }
            else if (context.Request["k"] != null && context.Request["k"] == "other" && context.Request["catid"] != null && context.Request["catid"].ToString() != "" && 
                        context.Request["newsid"] != null && context.Request["newsid"].ToString() != "")
            {
                int top = 5, catid=0;
                long newsid = 0;
                if (context.Request["top"] != null && context.Request["top"].ToString() != "")
                {
                    top = Convert.ToInt32(context.Request["top"]);
                }

                catid = Convert.ToInt32(context.Request["catid"].ToString());
                newsid = Convert.ToInt64(context.Request["newsid"].ToString());

                value = NewsOtherList(newsid, top, catid);
            }
            else if (context.Request["k"] != null && context.Request["k"] == "new")
            {
                value = GetTinMoiNhat(0,10);
            }

            //context.Response.Charset = "utf-8";
            context.Response.ContentType = "application/json";
            context.Response.Write(value);
        }

        public static string GetNewsList(string catid, Int32 pageindex, Int32 pagesize)
        {
            string value = "";

            NewsClass.NewsList newsList = new NewsClass.NewsList();
            newsList = Data.GetNewsList(catid, pageindex, pagesize);
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            value = jsSerializer.Serialize(newsList);

            return value;
        }

        public static string GetTinMoiNhat(int Cat_ID, int Top)
        {
            string value = "";

            NewsClass.NewsList newsList = new NewsClass.NewsList();
            newsList = Data.GetTinMoiNhat(Cat_ID, Top);
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            value = jsSerializer.Serialize(newsList);

            return value;
        }

        public static string GetNewsDetail(long newsid, int topOther)
        {
            NewsClass.NewsContent newsContent = new NewsClass.NewsContent();
            newsContent = Data.GetContent(newsid, topOther);
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            string value = jsSerializer.Serialize(newsContent);
            return value;
        }

        public static string GetAllCategory()
        {
            /*
            NewsClass.Category category = new NewsClass.Category();
            category = Data.GetAllCategory();
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            string value = jsSerializer.Serialize(category);
            return value;
            */

            NewsClass.CategoryAll category = new NewsClass.CategoryAll();
            category = Data.GetCategoryList();
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            string value = jsSerializer.Serialize(category);
            return value;
        }

        public static string Search(string keysearch, int pageindex, int pagesize)
        {
            string value = "";

            NewsClass.NewsList newsList = new NewsClass.NewsList();
            newsList = Data.Search(keysearch, pageindex, pagesize);
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            value = jsSerializer.Serialize(newsList);

            return value;
        }

        public static string NoiBatTrangChu(int top)
        {
            string value = "";

            NewsClass.NewsList newsList = new NewsClass.NewsList();
            newsList = Data.NoiBatTrangChu(top);
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            value = jsSerializer.Serialize(newsList);

            return value;
        }

        public static string NewsOtherList(long newsid, int top, int catid)
        {
            string value = "";

            NewsClass.NewsList newsList = new NewsClass.NewsList();
            newsList = Data.NewsOtherList(newsid, top, catid);
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            value = jsSerializer.Serialize(newsList);

            return value;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}